package com.system.action;

import java.io.File;
import java.io.FileInputStream;
import java.util.List;
import java.util.UUID;

import javax.annotation.Resource;

import jxl.Workbook;

import org.apache.struts2.ServletActionContext;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.sun.org.apache.regexp.internal.recompile;
import com.system.entity.Doc;
import com.system.entity.DocFile;
import com.system.entity.DocType;
import com.system.service.DocFileService;
import com.system.service.DocTypeService;
import com.system.tools.DocFileTool;
import com.system.tools.FileOperater;

@Controller
public class DocFileAction extends ActionSupport implements
		ModelDriven<DocFile> {

	@Resource
	private DocFileService docFileService;
	@Resource
	private DocTypeService docTypeService;
	private String message;
	private File upload;
	private String savePath;// 保存路径，接收xml的配置
	private String uploadContentType;// 类型
	private String uploadFileName;// 上传文件名
	private DocFile docFile;

	@Override
	public DocFile getModel() {
		if (docFile == null) {
			docFile = new DocFile();
		}
		return docFile;
	}

	public String doc_file_add() {
		if (upload == null) {
			setMessage("请选择要上传的文件！");
			return INPUT;
		}
		int type_id = Integer.parseInt(ServletActionContext.getRequest()
				.getParameter("type_id"));
		DocType docType = docTypeService.queryById(type_id);
		docFile.setDocType(docType);
		try {
			docFile.setDoc_title(uploadFileName);

			// docFileService.add(docFile);
			String filepath = UUID.randomUUID().toString()
					+ uploadFileName.substring(uploadFileName.lastIndexOf("."));
			String path = ServletActionContext.getRequest().getRealPath("/")
					+ getSavePath() + filepath;
			File file = new File(path);
			org.apache.commons.io.FileUtils.copyFile(upload, file);
			docFile.setFile_path(filepath);
			docFileService.add(docFile);
		} catch (Exception e) {
			// TODO: handle exception
		}
		return SUCCESS;
	}

	public String docFile_list() {
		List<DocFile> docFiles = null;
		docFiles = docFileService.queryAll();
		ActionContext.getContext().getSession().put("docFiles", docFiles);
		return SUCCESS;
	}

	public String docfile_delete() {
		docFile = docFileService.queryById(docFile.getDocFile_id());
		docFileService.delete(docFile);
		String filePath = docFile.getFile_path();
		String basePath = ServletActionContext.getRequest().getRealPath("/")
				+ "files\\";
		String filePathString = basePath + filePath;
		System.err.println(filePath);
		System.err.println(basePath);
		System.err.println(filePathString);
		FileOperater.deleteFile(filePathString);
		return SUCCESS;
	}

	public String getDocFileListByType() {
		int type_id = Integer.parseInt(ServletActionContext.getRequest()
				.getParameter("type_id"));
		DocType docType = docTypeService.queryById(type_id);
		// DocType docType = docTypeService.queryByType(DocType docType);
		List<DocFile> docFiles = null;
		docFiles = docFileService.queryByType(docType);
		ActionContext.getContext().getSession().put("docFiles", docFiles);
		return SUCCESS;
	}

	public String read_online() throws Exception {
		DocFile docFile = docFileService.queryById(this.docFile.getDocFile_id());
		String basePath = ServletActionContext.getRequest().getRealPath("/");
		File file = new File(basePath + "files\\" + docFile.getFile_path());
		String fileName=file.getName();
	    String type=fileName.substring(fileName.lastIndexOf(".")+1);
		//String type = file.getName().substring(file.getName().lastIndexOf(\".\")+1,fileName.length())
		Doc doc = new Doc();
		
		doc.setAuthor(docFile.getAuthor());
		doc.setCreate_time(docFile.getCreate_time());
		doc.setDoc_title(docFile.getDoc_title());
		doc.setDocType(docFile.getDocType());
		if (type.equals("doc")) {
			doc.setDoc_content(DocFileTool.getWordContent(file));
		}else if (type.equals("xls")) {
			
			doc.setDoc_content(DocFileTool.getExcelContent(file));
			FileInputStream fis = new FileInputStream(file);
			jxl.Workbook workbook = Workbook.getWorkbook(fis);
			ActionContext.getContext().getSession().put("workbook", workbook);
			
			return "xls";
		}else if(type.equals("pdf")){
			doc.setDoc_content(DocFileTool.getPDFContent(file));
			doc.setDoc_title(docFile.getDoc_title());
			doc.setDocType(docFile.getDocType());
			ActionContext.getContext().getSession().put("doc", doc);
			
			String pdfpath = docFile.getFile_path();
			ActionContext.getContext().getSession().put("pdfpath", pdfpath);
			System.out.println(pdfpath);
			return "pdf";
		}else {
			return INPUT;
		}
		
		ActionContext.getContext().getSession().put("doc", doc);
		return SUCCESS;
	}
	
	public File getUpload() {
		return upload;
	}

	public void setUpload(File upload) {
		this.upload = upload;
	}

	public String getUploadContentType() {
		return uploadContentType;
	}

	public void setUploadContentType(String uploadContentType) {
		this.uploadContentType = uploadContentType;
	}

	public String getUploadFileName() {
		return uploadFileName;
	}

	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public String getSavePath() {
		return savePath;
	}

	public void setSavePath(String savePath) {
		this.savePath = savePath;
	}

}
